#include "loader.cxx"

/*
  single method for loading different sample collection 
*/
TFileCollection* load_collection(const char* sample) {

    TFileCollection *c;

    if (!TString(sample).CompareTo("data10")) {
        cout << "load data10 sample" << endl;
        c = getOneDataCollection("muon","2010","muon");
    }

    if (!TString(sample).CompareTo("data10_test")) {
        cout << "load data10 periodG sample" << endl;
        c = getOneDataCollection("muon","periodG","muon");
    }

    if (!TString(sample).CompareTo("mc_ttbar")) {
        cout << "load mc ttbar sample" << endl;
        c = getOneMCCollection("","ttbar","nominal");
    }

    if (!TString(sample).CompareTo("mc_stop")) {
        cout << "load mc stop sample" << endl;
        c = getOneMCCollection("","singletop","nominal");
    }

    if (!TString(sample).CompareTo("mc_wjets")) {
        cout << "load mc wjets sample" << endl;
        c = getOneMCCollection("","wjets","nominal");
    }

    if (!TString(sample).CompareTo("mc_zjets")) {
        cout << "load mc zjets sample" << endl;
        c = getOneMCCollection("","zjets","nominal");
    }

    if (!TString(sample).CompareTo("mc_diboson")) {
        cout << "load mc diboson sample" << endl;
        c = getOneMCCollection("","diboson","nominal");
    }

    if (!TString(sample).CompareTo("mc_qcd")) {
        cout << "load mc qcd sample" << endl;
        c = getOneMCCollection("","qcd","nominal");
    }

    return c;
}

/*
  single method for loading different sample tree
*/
TChain* load_sample(const char* sample) {

    TChain *t;

    if (!TString(sample).CompareTo("data10")) {
        cout << "load data10 sample" << endl;
        t = getDataChain("muon","muon");
    }

    if (!TString(sample).CompareTo("data10_test")) {
        cout << "load data10 periodG sample" << endl;
        t = getOneDataChain("muon","periodG","muon");
    }

    if (!TString(sample).CompareTo("mc_ttbar")) {
        cout << "load mc ttbar sample" << endl;
        t = getOneMCChain("","ttbar","nominal");
    }

    if (!TString(sample).CompareTo("mc_stop")) {
        cout << "load mc stop sample" << endl;
        t = getOneMCChain("","singletop","nominal");
    }

    if (!TString(sample).CompareTo("mc_wjets")) {
        cout << "load mc wjets sample" << endl;
        t = getOneMCChain("","wjets","nominal");
    }

    if (!TString(sample).CompareTo("mc_zjets")) {
        cout << "load mc zjets sample" << endl;
        t = getOneMCChain("","zjets","nominal");
    }

    if (!TString(sample).CompareTo("mc_diboson")) {
        cout << "load mc diboson sample" << endl;
        t = getOneMCChain("","diboson","nominal");
    }

    if (!TString(sample).CompareTo("mc_qcd")) {
        cout << "load mc qcd sample" << endl;
        t = getOneMCChain("","qcd","nominal");
    }

    if (!TString(sample).CompareTo("mc_qcdbb")) {
        cout << "load mc qcdbb sample" << endl;
        t = getOneMCChain("","qcdbb","nominal");
    }

    if (!TString(sample).CompareTo("mc_mix")) {
        cout << "load mc mix sample" << endl;
        t = getMCChain("","nominal");
    }

    return t;
}

TH1* load_histogram(const char* sample, TString flavor, TString observable, TString cut, Int_t nbins, Double_t lowx, Double_t highx) {

    TH1* h;

    if (!TString(sample).CompareTo("data10")) {
        cout << "load data10 sample" << endl;
        h = loadAllHistData(flavor, observable, cut, nbins, lowx, highx, "muon");
    }
    
    if (!TString(sample).CompareTo("data10_test")) {
        cout << "load data10 periodG sample" << endl;
        h = loadOneHistData(flavor, "periodG", observable, cut, nbins, lowx, highx, "muon");
    }

    if (!TString(sample).CompareTo("mc_ttbar")) {
        cout << "load mc ttbar sample" << endl;
        h = loadOneHistMC(flavor, "ttbar", observable, cut, nbins,  lowx, highx); 
    }

    if (!TString(sample).CompareTo("mc_stop")) {
        cout << "load mc single top sample" << endl;
        h = loadOneHistMC(flavor, "singletop", observable, cut, nbins, lowx, highx);
    }

    if (!TString(sample).CompareTo("mc_wjets")) {
        cout << "load mc W+jets sample" << endl;
        h = loadOneHistMC(flavor, "wjets", observable, cut, nbins, lowx, highx);
    }

    if (!TString(sample).CompareTo("mc_zjets")) {
        cout << "load mc Z+jets sample" << endl;
        h = loadOneHistMC(flavor, "zjets", observable, cut, nbins, lowx, highx);
    }

    if (!TString(sample).CompareTo("mc_diboson")) {
        cout << "load mc diboson sample" << endl;
        h = loadOneHistMC(flavor, "diboson", observable, cut, nbins, lowx, highx);
    }

    return h;
}

/*
  return the integrated luminosity with unit pb^-1 
*/
Double_t getIntLumi(const char* sample) {

    Double_t lumi = 0.;

    if (!TString(sample).CompareTo("data10")) {
        lumi = 36.6;
    }

    if (!TString(sample).CompareTo("data10_test")) {
        // data10_test is periodG data: https://twiki.cern.ch/twiki/bin/view/AtlasProtected/DataPeriods
        lumi = 9.07;
    }

    return lumi;
}

/*
  register and verify a new dataset in proof 
*/
void reg_proof_dataset(const char* sample, const char* dsname) {

    TFileCollection *c = load_collection(sample);

    TProof *p = TProof::Open("workers=0");

    p->RegisterDataSet(dsname, c);
    p->VerifyDataSet(dsname);
    p->ShowDataSets();
    p->Close();

    delete c;
    delete p;
}
